package com.century.sjt.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.util.Constant;
import com.century.sjt.util.QRBitmapUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;
import com.google.zxing.WriterException;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class ShopQRRecordActivity extends Activity {
    private SjtTitleBar titleBar;
    private ImageButton btnSys;                                     //返回按钮
    private TextView mTitle;                                        //程序标题
    private LinearLayout leftLinLayout;                             //左边的线性布局

    private ImageView imgQR;
    protected int mScreenWidth ;

    private ImageView userImg;
    private TextView userName;

    private Handler mHandler;
    private RequestQueue mQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_qrrecord);
        mQueue = Volley.newRequestQueue(ShopQRRecordActivity.this);
//        mTitle= (TextView) findViewById(R.id.img_title);
//        mTitle.setText("二维码名片");
//        btnSys= (ImageButton) findViewById(R.id.btn_sys);
////        btnSys.setImageResource(R.drawable.selector_btn_return);
//        btnSys.setClickable(false);
//        leftLinLayout= (LinearLayout) findViewById(R.id.leftLinLayout);
//        leftLinLayout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                finish();
//                overridePendingTransition(R.animator.in_from_left, R.animator.out_to_right);
//            }
//        });
        titleBar = (SjtTitleBar) findViewById(R.id.shop_qr_title);
        //       标题栏
        titleBar.setLeftIconResource(getResources().getString(R.string.icon_return));
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        titleBar.setTitle(getResources().getString(R.string.tab_shop_qr_number));

        imgQR= (ImageView) findViewById(R.id.mqrrimage);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;

//        userImg = (ImageView) findViewById(R.id.qrc_userimg);
//        userName = (TextView) findViewById(R.id.qrc_username);
//
//        SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
//        userName.setText(mainSharedPreferences.getString("nickName", "用户"));
//        final String userHeaderImg=mainSharedPreferences.getString("image","");
//        if(!userHeaderImg.equals("")){
//                FileUtil.setupBigImage(userHeaderImg, mQueue, userImg);
//        }
        Create2QR();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my_qrrecord, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void Create2QR(){
        String uri = "shopId:"+getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE).getString("shopId", "");
//		Bitmap bitmap = BitmapUtil.create2DCoderBitmap(uri, mScreenWidth/2, mScreenWidth/2);
        Bitmap bitmap;
        try {
            bitmap = QRBitmapUtil.createQRCode(uri, mScreenWidth);

            if(bitmap != null){
                imgQR.setImageBitmap(bitmap);
//                imgQR.startAnimation(AnimationUtils.loadAnimation(MyQRRecordActivity.this,R.anim.navigation));   效果不明显，所以取消了
            }

        } catch (WriterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }

    }

    private boolean isFileExist(String fileName){
        boolean exist=false;
        if(!fileName.equals("")){
            File file=new File(Constant.CAMERA_PICTURE_PATH, fileName);
            if(file.exists()){
                exist=true;
            }
        }
        return exist;
    }

    private void createImageDir(){
        File file=new File(Constant.SYSTEM_CACHE_PATH);
        if(!file.exists())file.mkdir();
        File file1=new File(Constant.CAMERA_PICTURE_PATH);
        if(!file1.exists())file1.mkdir();
    }

    private void saveUserImg(Bitmap bitmap,String fileName){                            //保存用户头像到指定文件夹，指定名称。
        if(!fileName.equals("")) {
            File f = new File(Constant.CAMERA_PICTURE_PATH, fileName);
            if (f.exists()) {
                f.delete();
            }
            try{
                FileOutputStream out = new FileOutputStream(f);
                bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }

}
